package org.codetranslators.compiler.liveness;

import java.util.Vector;

public class Node 
{
	private Graph parentGraph;
    private int nodeKey;
    private String id;
    
    private Vector successors;
    private Vector predecessors;
    
	private Node(){}

    public Node(Graph g, String id)
    {
    	successors = new Vector();
    	predecessors = new Vector();
    	
		parentGraph = g; 
		this.id = id;
		g.incrementNodeCount();
		
		g.addNode(this, Integer.parseInt(id));
    }

    public void addToSuccessorList(Node succ)
    {
    	successors.addElement(succ);
    }
    
    public void addToPredecessorList(Node pred)
    {
    	predecessors.addElement(pred);
    }
    
    public Vector getSuccessors() 
    {
    	return successors;
    }
    
    public Vector getPredecessors()
    {
    	return predecessors;
    }
    
    public int inDegree() 
    {
    	return predecessors.size();
    }
    
    public int outDegree() 
    {
    	return successors.size();
    }
    
    public int degree()
    {
    	return inDegree()+outDegree();	
    } 

    private boolean hasNode(Node n, Vector nodeList) {
    	int numSuccs = nodeList.size();
    	for(int i = 0; i < numSuccs; i++)
    	{
    		Node nodeFound = (Node) nodeList.elementAt(i);
    		if(nodeFound == n)
    			return true;
    	}
    	return false;
    }
    
    public boolean goesTo(Node n)
    {
    	return hasNode(n, successors);
    }

    public boolean comesFrom(Node n)
    {
    	return hasNode(n, predecessors);
    }

    public boolean adj(Node n) 
    {
    	return goesTo(n) || comesFrom(n);
    }

    public String toString() 
    {
    	return String.valueOf(nodeKey);
    }
    
    public Graph getGraph()
    {
    	return parentGraph;
    }

	public String getId() 
	{
		return id;
	}

}
